import 'package:flutter/material.dart';
import 'package:flutter_animator/flutter_animator.dart';
import 'package:get/get.dart';

import '../../resources/resources.dart';
import '../my_text.dart';

class MyDialog extends StatelessWidget {
  MyDialog({
    Key? key,
    this.title,
    this.backgroundColor,
    this.middleText,
    this.leftButtonText = "取消",
    this.rightButtonText = "确定",
    this.middleButtonText = "OK",
    this.rightButtonColor = Colors.transparent,
    this.leftButtonColor = Colors.transparent,
    this.rightButtonTextColor,
    this.leftButtonTextColor = Colors.grey,
    this.middle,
    this.onLeftTap,
    this.onRightTap,
    this.onMiddleTap,
  }) : super(key: key) {
    if (title != null) {
      titleWidget = MyText(
        title!,
        weight: FontWeight.bold,
        size: 17,
        color: JmColors.textColor,
      );
    }

    if (middle != null) {
      contentWidget = middle!;
    } else if (middleText != null) {
      contentWidget = MyText(
        middleText!,
        size: 14,
        color: Colors.grey[850],
      );
    }
    if (onLeftTap == null && onMiddleTap == null && onRightTap == null) {
      actions.add(Expanded(
        child: button(
            text: "确定",
            onTap: () {
              Get.back();
            }),
      ));
    } else {
      if (onLeftTap != null && onMiddleTap != null && onRightTap != null) {
        actions.add(
          SizedBox(
            width: double.infinity,
            child: button(
                text: leftButtonText,
                color: leftButtonColor,
                textColor: leftButtonTextColor,
                onTap: onLeftTap!),
          ),
        );
        actions.add(SizedBox(
            width: double.infinity,
            child: button(text: middleButtonText, onTap: onMiddleTap!)));
        actions.add(SizedBox(
            width: double.infinity,
            child: button(
                text: rightButtonText,
                onTap: onRightTap!,
                color: rightButtonColor,
                textColor: rightButtonTextColor)));
      } else {
        if (onLeftTap != null) {
          actions.add(Expanded(
              child: button(
                  text: leftButtonText,
                  color: leftButtonColor,
                  textColor: leftButtonTextColor,
                  onTap: onLeftTap)));
        }
        if (onRightTap != null) {
          actions.add(Expanded(
              child: button(
            text: rightButtonText,
            onTap: onRightTap,
            color: rightButtonColor,
            textColor: rightButtonTextColor,
          )));
        }
      }
    }
  }

  List<Widget> actions = [];
  Widget? titleWidget;
  Widget? contentWidget;

  final String? title;
  final String? middleText;
  final Color? backgroundColor;

  final String leftButtonText;
  final String rightButtonText;
  final String middleButtonText;

  final Color leftButtonColor;
  final Color rightButtonColor;
  final Color leftButtonTextColor;
  final Color? rightButtonTextColor;

  final Widget? middle;
  final VoidCallback? onLeftTap;
  final VoidCallback? onRightTap;
  final VoidCallback? onMiddleTap;

  @override
  Widget build(BuildContext context) {
    return ZoomIn(
      child: AlertDialog(
        contentPadding:
            const EdgeInsets.only(left: 20, right: 20, top: 8, bottom: 12),
        backgroundColor: backgroundColor ?? Colors.white,
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(10))),
        content: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: [
            const SizedBox(height: 14),
            //contentWidget,
            titleWidget ?? Container(),
            contentWidget != null ? const SizedBox(height: 14) : Container(),
            contentWidget ?? Container(),
            contentWidget != null ? const SizedBox(height: 14) : Container(),
            const SizedBox(height: 8),
            actions.length < 3
                ? Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: actions,
                  )
                : Column(
                    children: actions,
                  ),
          ],
        ),
        // actions: actions, // ?? <Widget>[cancelButton, confirmButton],
        buttonPadding: EdgeInsets.zero,
      ),
      preferences: const AnimationPreferences(
        duration: Duration(milliseconds: 500),
      ),
    );
  }

  Widget button({
    required String text,
    Color? color,
    Color? textColor,
    VoidCallback? onTap,
  }) {
    return TextButton(
      onPressed: onTap,
      child: MyText(
        text,
        color: textColor ?? Colors.blueAccent,
        size: 16,
        weight: FontWeight.normal,
      ),
      style: ButtonStyle(
        backgroundColor:
            MaterialStateProperty.all<Color>(color ?? Colors.transparent),
        shape: MaterialStateProperty.all(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
        ),
      ),
    );
  }
}
